﻿using System;
using System.Configuration;

namespace Common
{
    public static class ImageLoadHelper
    {
        private const string _narrow = "Narrow/";
        private const string _original = "Original/";
        private static readonly string _imageLoadUrl = ConfigurationManager.AppSettings["ImageLoadUrl"];

        private static string GetPrevNarrowImageUrl()
        {
            return _imageLoadUrl + _narrow;
        }

        private static string GetPrevOriginalImageUrl()
        {
            return _imageLoadUrl + _original;
        }

        public static string GetNarrowImageUrl(int id)
        {
            return GetPrevNarrowImageUrl() + id;
        }

        public static string GetOriginalImageUrl(int id)
        {
            return GetPrevOriginalImageUrl() + id;
        }

        public static int ZoomHeight(int width, int height, int expectWidth, int expectHeight)
        {
            if (expectHeight == 0 || expectWidth == 0)
            {
                return height;
            }
            var ew = (double) expectWidth;
            var eh = (double) expectHeight;
            double rate = ew/width;
            double rate2 = eh/height;
            if (rate > rate2)
            {
                rate = rate2;
            }
            return (int) Math.Round(height*rate, MidpointRounding.AwayFromZero);
        }

        public static int ZoomWidth(int width, int height, int expectWidth, int expectHeight)
        {
            if (expectHeight == 0 || expectWidth == 0)
            {
                return width;
            }
            var ew = (double) expectWidth;
            var eh = (double) expectHeight;
            double rate = ew/width;
            double rate2 = eh/height;
            if (rate > rate2)
            {
                rate = rate2;
            }
            return (int) Math.Round(width*rate, MidpointRounding.AwayFromZero);
        }
    }
}